package com.ytjj.qmyx.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.GraduateProductTypeEnums;
import com.ytjj.qmyx.mall.enums.SpecialIsGraduateEnums;
import com.ytjj.qmyx.mall.enums.SpecialStatusEnums;
import com.ytjj.qmyx.mall.enums.SpecialTypeEnums;
import com.ytjj.qmyx.mall.mapper.SpecialDao;
import com.ytjj.qmyx.mall.mapper.SpecialMapper;
import com.ytjj.qmyx.mall.model.FrontCategory;
import com.ytjj.qmyx.mall.model.Special;
import com.ytjj.qmyx.mall.model.SpecialExample;
import com.ytjj.qmyx.mall.model.UsersFake;
import com.ytjj.qmyx.mall.model.response.GraduateProductResponse;
import com.ytjj.qmyx.mall.model.response.ProductResponse;
import com.ytjj.qmyx.mall.model.response.SpecialResponse;
import com.ytjj.qmyx.mall.service.CommonService;
import com.ytjj.qmyx.mall.service.ProductService;
import com.ytjj.qmyx.mall.service.SpecialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 专栏接口实现
 *
 * 
 * @version 1.0
 *
 */
@Service
public class SpecialServiceImpl implements SpecialService {

    private final ProductService productService;

    private final CommonService commonService;

    @Resource
    private SpecialMapper specialMapper;

    @Resource
    private SpecialDao specialDao;

    public SpecialServiceImpl(ProductService productService, CommonService commonService) {
        this.productService = productService;
        this.commonService = commonService;
    }

    @Override
    public List<SpecialResponse> getSpecialListByType(String type, Integer isHomeShow) {
//        SpecialExample example = new SpecialExample();
//        example.createCriteria()
//                .andTypeEqualTo(type)
//                .andIsHomeShowEqualTo(isHomeShow.byteValue())
//                .andStatusEqualTo(SpecialStatusEnums.INVOKE.getValue())
//        ;

        List<SpecialResponse> list = specialDao.findList(type,isHomeShow);
        return list;
//        List<Special> specialList = specialMapper.selectByExample(example);
//        return specialList.stream()
//                .map(SpecialResponse::new)
//                .collect(Collectors.toList());


    }

    @Override
    public List<SpecialResponse> getSpecialListByType(String type) {
        SpecialExample example = new SpecialExample();
        example.createCriteria()
                .andTypeEqualTo(type)
                .andStatusEqualTo(SpecialStatusEnums.INVOKE.getValue());
        List<Special> specialList = specialMapper.selectByExample(example);
        return specialList.stream()
                .map(item -> new SpecialResponse(item, null))
                .collect(Collectors.toList());
    }

    @Override
    public List<SpecialResponse> getGraduateSpecialListByType(String type) {
        SpecialExample example = new SpecialExample();
        example.createCriteria()
                .andTypeEqualTo(type)
                .andStatusEqualTo(SpecialStatusEnums.INVOKE.getValue())
                .andIsGraduateEqualTo(SpecialIsGraduateEnums.YES.getValue());
        List<Special> specialList = specialMapper.selectByExample(example);
        return specialList.stream()
                .map(item -> {
                    List<ProductResponse> productResponses =
                            productService.getProductListBySpecialId(item.getId(), 1, 3);
                    return new SpecialResponse(item, productResponses);
                })
                .collect(Collectors.toList());
    }

    @Override
    public SpecialResponse getSpecialInfo(Integer specialId) {
        return new SpecialResponse(specialMapper.selectByPrimaryKey(specialId));
    }

    @Override
    public GraduateProductResponse getGraduateProductList(Integer specialId) {
        GraduateProductResponse response = new GraduateProductResponse();
        Special special = specialMapper.selectByPrimaryKey(specialId);
        response.setSpecialResponse(new SpecialResponse(special));
        response.setRecommendProductList(productService.getGraduateProductList(specialId, GraduateProductTypeEnums.RECOMMEND.name()));
        response.setLikeProductList(productService.getGraduateProductList(specialId, GraduateProductTypeEnums.LIKE.name()));
        return response;
    }

    @Autowired
    private RedisService redisService;

    @Override
    public List<SpecialResponse> getRecommendSpecialList() {
        SpecialExample example = new SpecialExample();
        example.createCriteria()
                .andTypeEqualTo(SpecialTypeEnums.recommend.name())
                .andStatusEqualTo(SpecialStatusEnums.INVOKE.getValue());
        List<Special> specialList = specialMapper.selectByExample(example);

        return specialList.stream()
                .map(item ->{
                    SpecialResponse specialResponse = new SpecialResponse(item);
                    List<Object> fakeUserList = redisService.range(RedisConstants.FAKE_USER, 0, -1);
                    Collections.shuffle(fakeUserList);
                    List<UsersFake> list = fakeUserList.stream()
                            .map(userItem -> JSONObject.parseObject((String) userItem, UsersFake.class))
                            .limit(3)
                            .collect(Collectors.toList());
                    specialResponse.setUsersFakeList(list);
                    return specialResponse;
                }).collect(Collectors.toList());
    }



//    @Override
//    public List<ProductResponse> getRecommendProductList(Integer specialId, UsersResponse usersResponse) {
//        List<ProductResponse> productResponseList = productService.getRecommendProductList(specialId, usersResponse);
//        productResponseList.stream()
//                .map(item -> {
//                    item.setRecommendImgList(productService.getRecommendImgById(item.getId()));
//                    return item;
//                }).collect(Collectors.toList());
//        return productResponseList;
//    }
}
